package com.jiang.myfm.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jiang.myfm.R;
import com.jiang.myfm.entities.billboard.DataList;
import com.jiang.myfm.utils.CircleTransformation;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by Mr.Jiang on 2016/12/11.
 */
public class BillboardInfoItemAdapter extends RecyclerView.Adapter<BillboardInfoItemAdapter.ViewHolder>implements View.OnClickListener{

    private Context context;
    private List<DataList> datasList;
    private onChildClickListener listener;
    private RecyclerView recyclerView;

    public BillboardInfoItemAdapter(Context context) {
        super();
        this.context = context;
        datasList = new ArrayList<>();
    }

    public void setListener(onChildClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        this.recyclerView = recyclerView;
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        this.recyclerView = null;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.billboard_info_item,parent,false);
        view.setOnClickListener(this);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.rankingTextView.setText(position+1+"");
        holder.titleTextView.setText(datasList.get(position).getTitle());
        holder.nicknameTextView.setText(datasList.get(position).getNickname());
        Picasso.with(context).load(datasList.get(position).getCoverSmall()).transform(new CircleTransformation()).into(holder.coversmallImageView);
    }

    public void addAll(Collection<? extends  DataList> collection){
        datasList.addAll(collection);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datasList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{

        private TextView rankingTextView;
        private ImageView coversmallImageView;
        private TextView titleTextView;
        private TextView nicknameTextView;

        public ViewHolder(View itemView) {
            super(itemView);
            rankingTextView = ((TextView) itemView.findViewById(R.id.ranking_tv));
            coversmallImageView = ((ImageView) itemView.findViewById(R.id.coversmall_iv));
            titleTextView = ((TextView) itemView.findViewById(R.id.title_tv));
            nicknameTextView = ((TextView) itemView.findViewById(R.id.nickname_tv));
        }
    }


    @Override
    public void onClick(View view) {
        if (recyclerView != null && listener != null){
            int position = recyclerView.getChildAdapterPosition(view);
            String trackId = datasList.get(position).getTrackId();
            String uid = datasList.get(position).getUid();
            listener.onChildClick(trackId,uid);
        }
    }

    public interface onChildClickListener{
        void onChildClick(String trackId,String uid);
    }
}
